package sapling.logic.algorithm.pcb.dispatcher;

import sapling.logic.object.pcb.ProcessControlBlock;
import sapling.main.OSEnvironment;

public class TSRDispatcher extends Dispatcher {
	public TSRDispatcher(OSEnvironment env) {
		super(env);
	}

	@Override
	protected boolean switchOver() {
		this.env.getPcbRunning().setLiveLong(this.env.getPcbRunning().getLiveLong() - 1);

		if (this.env.getPcbRunning().getLiveLong() <= 0) {
			this.env.getPcbRunning().release();
			if (this.env.getPcbQueue().isEmpty()) {
				this.env.setPcbRunning(null);
				return true;
			}
		}
		if (this.env.getPcbQueue().isEmpty())
			return false;
		ProcessControlBlock temp = this.env.getPcbQueue().dequeue();
		if (this.env.getPcbRunning().getLiveLong() > 0)
			this.env.getPcbQueue().enqueue(this.env.getPcbRunning());
		this.env.setPcbRunning(temp);
		return false;
	}
}
